package com.biz.primus.ms.sms.service;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.common.utils.CopyUtils;
import com.biz.primus.model.misc.enums.SMSType;
import com.biz.primus.model.misc.exception.SmsExceptions;
import com.biz.primus.model.misc.vo.SmsTemplateVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.sms.dao.po.SmsTemplate;
import com.biz.primus.ms.sms.dao.redis.SmsTemplateRedisDao;
import com.biz.primus.ms.sms.dao.repository.SmsTemplateRepository;
import com.biz.primus.ms.sms.dao.ro.SmsTemplateRo;
import com.biz.primus.ms.sms.transform.SmsTemplate2SmsTemplateRo;
import com.biz.primus.ms.sms.transform.SmsTemplate2SmsTemplateVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 类说明：短信模版 serviceImpl 实现
 *
 * @author xiaoyasong
 */
@Service
@Slf4j
public class SmsTemplateService extends AbstractBaseService {

    private final SmsTemplateRepository smsTemplateRepository;

    private final SmsTemplateRedisDao smsTemplateRedisDao;

    private SmsTemplate2SmsTemplateRo smsTemplate2SmsTemplateRo = new SmsTemplate2SmsTemplateRo();

    @Autowired
    public SmsTemplateService(SmsTemplateRepository smsTemplateRepository, SmsTemplateRedisDao smsTemplateRedisDao) {
        this.smsTemplateRepository = smsTemplateRepository;
        this.smsTemplateRedisDao = smsTemplateRedisDao;
    }

    public List<SmsTemplateVo> allTemplate() {
        List<SmsTemplate> all = smsTemplateRepository.findAll();
        return Lists.transform(all, new SmsTemplate2SmsTemplateVo());
    }

    /**
     * 从数据库中查短信
     */
    public SmsTemplateVo getSmsTemplate(SMSType type) {
        AssertUtils.notNull(type, SmsExceptions.SMS_TYPE_IS_NULL);
        SmsTemplate smsTemplate = smsTemplateRepository.findByType(type);
        return new SmsTemplate2SmsTemplateVo().apply(smsTemplate);
    }

    /**
     * 将一个短信模板渲染成一条完整的短信信息
     */
    public String intactMessage(SMSType type, Map<String, String> params) {
        SmsTemplateRo ro = smsTemplateRedisDao.getByType(type);
        AssertUtils.notNull(ro, SmsExceptions.SMS_TEMPLATE_NOT_FOUND);
        String templateContent = ro.getTemplateContent();
        Set<String> keys = params.keySet();
        for (String key : keys) {
            templateContent = templateContent.replaceAll("\\{" + key + "\\}", params.get(key));
        }
        return templateContent;
    }

    /**
     * 短信模版 保存以及修改的功能
     */
    public void saveOrUpdateSmsTemplate(SmsTemplateVo vo) {
        String idStr = vo.getId();
        SmsTemplate smsTemplate;
        if (StringUtils.isBlank(idStr)) {
            smsTemplate = new SmsTemplate();
            smsTemplate.setId(idService.getNextId());
        } else {
            Long id = Long.valueOf(idStr);
            smsTemplate = smsTemplateRepository.findOne(id);
            AssertUtils.notNull(smsTemplate, SmsExceptions.SMS_TEMPLATE_NOT_FOUND);
        }
        CopyUtils.copyProperties(vo, smsTemplate);
        saveOrUpdateUsingPo(smsTemplateRepository, smsTemplateRedisDao, smsTemplate, new SmsTemplate2SmsTemplateRo());
    }

    /**
     * 同步短信模板
     */
    public void sync2Redis() {
        List<SmsTemplate> all = smsTemplateRepository.findAll();
        List<SmsTemplateRo> ros = Lists.transform(all, smsTemplate2SmsTemplateRo);
        smsTemplateRedisDao.save(ros);
    }
}
